
var moment = require("moment");
var fs = require("fs");
var path = require("path");
var ajax = require("../ajax/ajax");
exports.up = function (req, res) {

    var formidable = require('formidable')


    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';		//设置编辑




    form.uploadDir = 'src/upload/' + moment().format("YYYYMMDD");	 //设置上传目录






    mkdirsSync(form.uploadDir);



    form.keepExtensions = true;	 //保留后缀
    form.maxFieldsSize = 50 * 1024 * 1024;   //文件大小
    form.parse(req, function (err, fields, files) {
        var f = null
        if (files.file) {
            f = files.file
        }
        else {
            f = files
        }

        var val = {};




        val.file = {

            name: f.name,
            url: ("/" + f.path).replace(/\\/g, "/").replace("src/", ""),
            query: req.query


        };





        ajax.re(res, {

            err: err,
            val: val
        })


    });





}


function mkdirsSync(dirname) {

    //console.log(dirname);
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirsSync(path.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    }
}